<template>
    <div class="bluetooth">
        <van-nav-bar safe-area-inset-top placeholder title="蓝牙" left-arrow fixed @click-left="onClickLeft" right-text="重新扫描" @click-right="scans" />
        <div class="tips">* 请确保您的手机已开启蓝牙，并且可以正常使用！</div>
        <!-- <div class="titleBox">蓝牙列表：</div> -->
        <van-divider content-position="left" :style="{borderColor: '#DADADA'}">蓝牙列表</van-divider>
        <van-cell-group style="text-align:left;margin-bottom:10px"  v-for="(data,i) in lists" :key="i">
            <van-cell :title="data.name">
                <div>
                    <van-button round type="primary" size="small" @click="connect(data.id)">连接</van-button>
                </div>
            </van-cell>
        </van-cell-group>
    </div>
</template>

<script>
import { Toast } from 'vant';
export default {
    data() {
        return {
            lists:[]
        };
    },
    methods: {
        onClickLeft(){
            this.$router.push({path:'/localin'});
        },
        //扫描去重
        uniqueArray(array, key){
            var result = [array[0]];
            for(var i = 1; i < array.length; i++){
            var item = array[i];
            var repeat = false;
            for (var j = 0; j < result.length; j++) {
                if (item[key] == result[j][key]) {
                repeat = true;
                break;
                }
            }
            if (!repeat) {
                result.push(item);
            }
            }
            return result;
        },
        //扫描蓝牙
        start(){
            this.comm.loadingShow();
            let arr=[];
            return new Promise((resolve)=>{
                ble.startScan([],(device)=>{
                    if(device.name==undefined||device.name==null){
                        device.name='***'
                    }
                    arr.push({'name':device.name,'id':device.id});
                }, (err)=>{
                    this.comm.toastHide();
                });
                //关闭扫描
                setTimeout(() => {
                    this.comm.toastHide();
                    ble.stopScan(()=>{
                        resolve(arr);
                    }, ()=>{
                        this.comm.toastHide();
                        console.log('error');
                    });
                }, 5000);
            });
        },
        scans(){
            this.start().then(datas=>{
                this.lists=this.uniqueArray(datas,'name');
            });
        },
        //连接蓝牙
        connect(device_id){
            ble.connect(device_id, (datas)=>{
                //获取characteristicUUID
                //let characters=datas.characteristics;
                // for(let i=0;i<characters.length;i++){
                //     if(characters[i].properties[0]=="writeWithoutResponse" && characters[i].service=='2B11'){
                //         characteristicId=characters[i].characteristic;
                //         service_uuid=characters[i].service;
                //         break;
                //     }
                // }
                Toast.success('连接成功');
                sessionStorage.setItem('device_id',device_id);
                this.$router.push({path:'/bluetooth/flow'});
            }, (err)=>{
                this.comm.tipsShow('连接失败!');
            });
        }
    },
    mounted() {
    this.scans();
  }
};
</script>

<style  lang="less" scoped>
.bluetooth{
    .tips{
        width: 100%;
        box-sizing: border-box;
        padding: 10px 10px 0 10px;
        font-size: 14px;
        color: #ff7a2c;
        text-align: left;
    }
    .titleBox{
        width: 100%;
        padding: 20px 10px;
        text-align: left;
        color: #777;
        box-sizing: border-box;
        font-size: 14px;
    }
}
</style>
